<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Validate;
use \app\common\model\ArticalRecord as ArticalRecordModel;

/**
 * 学生作文批改表
 */
class Articalrecord extends Api
{
    /**
     * 学生作文批改表
     * @var array
     */
    protected $noNeedRight = '*';
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ArticalRecordModel();
    }

      /**
     * 查询学生作文批改详情
     * @ApiTitle    (查询学生作文批改详情)
     * @ApiMethod  (GET)
     * @ApiParams (name="student_id", type="int", required=true, description="学生ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="查询成功")
     * @ApiReturn   ({
            'code':'200',
           'msg':'查询成功',
           'time': "",
           'data': $row
         })
     */
    public function getArticalRecord()
    {

        $data = input('get.');
        $studentId = $data['student_id'];
        if (!Validate::is($studentId, 'number') || !$studentId) {
            $this->error('学生ID错误');
        }

        $dis = [
            'student_id' => $studentId
        ];
        $rawData = $this->model->dataList($dis);

        if (!$rawData) {
            $this->success('未查询到学生作文批改记录', []);
        }

        // 处理图片数据并准备合并
        $processedData = [];
        $imageMap = []; // 用于记录已处理的图片组合

        foreach ($rawData as $item) {
            // 处理图片数组
            $images = array_filter(explode(',', $item['image']));
            $images = array_values($images);
            $imageKey = md5(implode(',', $images)); // 创建图片的唯一标识

            // 如果图片组合已存在，则合并数据
            if (isset($imageMap[$imageKey])) {
                // 合并其他字段（可根据需要调整合并逻辑）
                $processedData[$imageMap[$imageKey]]['create_time'] = max(
                    $processedData[$imageMap[$imageKey]]['create_time'],
                    $item['create_time']
                );
                // 可以添加其他需要保留的字段
            } else {
                // 添加新记录
                $newItem = $item;
                $newItem['image'] = $images;
                $processedData[] = $newItem;
                $imageMap[$imageKey] = count($processedData) - 1; // 记录索引位置
            }
        }

        // 重新索引数组
        $processedData = array_values($processedData);

        $this->success('查询成功', $processedData);
    }
    /**
     * 作文批改详情
     * @ApiTitle    (查询作文批改详情)
     *  @ApiMethod  (GET)
     *  @ApiParams   (name="id", type="int", required=true, description="作文ID")
     *  @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     *  @ApiReturnParams   (name="msg", type="string", required=true, sample="查询成功")
     *  @ApiReturn   ({
          'code':'200',
          'msg':'查询成功',
          'time': "",
          'data': $row
        })
     */
    public function getArticalRecordDetail(){

        $data = input('get.');
        $id = $data['id'];
        if (!Validate::is($id, 'number') || !$id) {
            $this->error('作文ID错误');
        }
        $dis = [
            'id' => $id,
        ];
        $row = $this->model->dataInfo($dis);
        if (!$row) {
            $this->success('未查询到作文批改记录', []); 
        }
        
        $row['image'] = array_filter(explode(',', $row['image']));
        $row['image'] = array_values($row['image']);

        $this->success('查询成功', $row);
    }
}
